<?php
/**
 * Created by PhpStorm.
 * User: hkun
 * Date: 2020/4/13
 * Time: 18:06
 */
namespace app\api\controller\v2\cms;
use think\Request;
use app\api\model\v2\cms\CmsUser as UserModel;
use app\api\controller\BaseController as BaseController;
use app\lib\enum\StateEnum;

use app\api\controller\SendEmail;
use think\Validate;

use think\Cache;
class CmsMail extends BaseController
{
	
	//邮箱配置
	private $mail = [
	    'host'        => 'smtp.163.com',//SMTP服务器地址
	    'send_email'  => 'a350839123@163.com',//发送邮件的邮箱账号
	    'password'    => 'CMYFEQSVIZNSSDYV',//发送邮件的邮箱密码（部分邮箱为授权码）
	];
	/*
	*	发送邮件
		@method:post
		* @param {varchar} email
		@url /api/v2/cms/sendMail
	*/
	public function sendMail(){
	    $data = request()->param();
	    //参数验证
	    $validate = new Validate(['email'=>'require|email']);
	    if(!$validate->check($data)){
	        return result(400,$validate->getError());
	    }
		$cacheKey = $data['email'];
		$count = Cache::get($cacheKey )?Cache::get($cacheKey ):0;
		if($count>5){
			return result(StateEnum::fail,'频繁访问，请六分钟之后重试');
		};
		Cache::set($cacheKey,$count+1,3600);
		
	    //生成验证码 -> 取出账号对应验证码
	    // $code = rand(10000,99999);
		$usermodel = new UserModel();
		$appid = $data['email'];
		$check_result = $usermodel->for_check($appid);
		$code = $check_result['code'];
	    //发送邮件
	    $SendMail = new SendEmail();
	    //邮件模板
	    $html = $SendMail->emailTemp($appid,$code);
	    //发送邮件
	    $r = $SendMail->send($data['email'],$this->mail,'验证码',$html);
	    if ($r){
	        // return result(StateEnum::success,'SUCCESS',['code'=>$code]);
	        return result(StateEnum::success,'SUCCESS',['code'=>1]);
	    }
	    return result(StateEnum::fail,'ERROR');
	}

} 